//测试数据，demoData.index表示index文件调用的数据，demoData.member表示member也需要调用的数据
var common = {
    "appKey": {
      siteAreaid: 'siteAreaid',
      pattern: 'pattern',
      userId: 'userId',
      userToken: 'userToken',
      appFWQ: 'appFWQ',
      ip:'ip',
      mac:"mac",
      apMac:'apMac',
      vipId:'vipid', //vip的Id
      pledge:'pledge',//存在文件vip 的商品的价格
      comboId:'comboId',
      good_tab:'good_tab',
      deviceCount:'deviceCount',
      package_List: 'package_List',
      letGoSate:'letGoSate',  //记录是否有放行
    },

    "keyword": {
      url_forwardPort: 'url_forwardPort',  //转接
      url_deviceList:'url_deviceList',    //在线设备列表
      network_offNet:'network_offNet',    //断网
      network_tempNet:'network_tempNet',   //临时上线
      binding:'binding',                  //绑定
      pcNetQr_pcQrselfNet:'pcNetQr_pcQrselfNet',  //授权pc联网
      pcNetQr_pcQRinfo:'pcNetQr_pcQRinfo',//获取二维码的信息
      vipdeposit:'/internet/tovip/vipdeposit.do', //购买会员、套餐
      updateDeviceName:'updateDeviceName',        //修改设备名字
      url_historyList:'url_forwardPort',
    },

    "appConstant":{
     linxTheserverIp:'http://119.29.54.250:8090',
    },

    //网络交换的提示
    getApiShowProgress: function() {
        api.showProgress({
            style: 'default',
            animationType: 'fade',
            title: '努力加载中...',
            text: '请稍等...',
            modal: false
        });

    },
    //关闭 网络交换的提示
    getApiHideProgress: function() {
        api.hideProgress();
    },

    ajax: function(requesturl,param,tiem,callback) {
        //封装了ajax请求
        api.ajax({
            url: requesturl,
            method: 'post',
            data: {
                values: param
            },
            timeout:tiem
        }, function(ret, err) {
            if (callback) {
                callback(ret, err);
            }
        });

    },
    //临时放行
    showNetworking:function(callback){
      var fwq = $api.getStorage(common.appKey.appFWQ);
      var parame = {
          userid: $api.getStorage(common.appKey.userId),
          siteAreaid:$api.getStorage(common.appKey.siteAreaid),
          userMac:$api.getStorage(common.appKey.mac),
          ip:$api.getStorage(common.appKey.ip),
          visitType:3
      }
      var parames = common.getJSONParame(common.keyword.network_tempNet,JSON.stringify(parame))
      common.getConsole('临时放行接口',JSON.stringify(parame))
      common.ajax($api.getStorage(common.appKey.appFWQ),parames,4,function(ret, err){
            common.getConsole('临时放行接口数据',JSON.stringify(ret))
           if(ret){
              if (ret.code == 1) {
                    callback(true)
              }else {
                    callback(false)
              }
           }else {
             callback(false)
           }
      })
    },

    //断网
    brokenNetwork:function(ip,mac,siteAreaid,callback){
      var fwq = $api.getStorage(common.appKey.appFWQ);
      var parame = {
          userid: $api.getStorage(common.appKey.userId),
          mac:mac,
          ip:ip,
          siteAreaid:siteAreaid
      }
      var parames = common.getJSONParame(common.keyword.network_offNet,JSON.stringify(parame))
      common.getConsole('断网接口',JSON.stringify(parame))
      common.ajax($api.getStorage(common.appKey.appFWQ),parames,8,function(ret, err){
           if(ret){
              common.getConsole('断网接口请求结果', JSON.stringify(ret));
              if (ret.code == 1) {
                 callback(true)
              }else {
                  callback(false)
              }
           }else {
               callback(false)
             common.getConsole('断网接口请求失败',JSON.stringify(err));
           }
      })
    },

    //判断云地址的联网状态(给外界调用)
    judgeCloudNetworkState:function(callback){
      api.ajax({
          url: 'http://123.207.110.98:8080/wifi/TransferServlet',
          method: 'post',
          data: {},
          timeout: 2
      }, function(ret,err) {
          if (ret) {
              if (ret.code <3 && ret.code >-3) {
                callback(true);
              }else {
                callback(false);
              }
          } else {
              callback(false);
          }
      });
    },

    //得到后台需要的请求参数
    getJSONParame: function(keyword, json) {
        var time = Date.parse(new Date());
        var token = $api.getStorage(common.appKey.userToken);
        var userid = $api.getStorage(common.appKey.userId);
        if (token == undefined) {
            token = 'null';
        }
        if (userid == undefined) {
            userid = '0';
        }
        var appType = 1;
        if(api.appVersion == '牛郎WiFi'){
                appType = 2;
        }
        var stringJson = "keyword=" + keyword + "&params=" + json + "&time=" + time + "&token=" + token + "&userid=" + userid + "&key=niulang2016";
        var md5sign = hex_md5(stringJson);
        var params = {
            params: json,
            token: token,
            userid: userid,
            time: time,
            keyword: keyword,
            sign: md5sign,
            apMac: $api.getStorage(common.appKey.apMac),
            apName: '',
            NF_callid: common.getRandom20(),
            appType: appType
        }
        return params;
    },


    //生成一个10位的随机数
    getRandom20: function() {
        var data = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
            var result = "";
            for (var i = 0; i < 10; i++) {
                r = Math.floor(Math.random() * 10);
                result += data[r];
            }
            return result;
    },


    /**显示或提示一些请求网络时发生的错误信息
    *code 错误状态码 msg 提示的信息
    *code 11:没有找到代理 21:500/404一般是服务器异常
    *0/1 服务器访问通过了 一些验证没有过   10 客户端定义的提示信息
    */
    getShowViewError:function(code,msg){
      switch (code) {
        case 11:
          alert(msg);
          break;
        case 21:case 500:case 404:
            if(msg == undefined){
              msg = '服务器异常'
            }
            getToast(msg,3000);
            break;
        case 0 :case -1:
               if(msg == '' || msg == undefined ){
                 msg = '服务器异常';
               }else {
                    var msgString = msg.substring(0,1);
                    if(msgString =='<' ){
                      msg = '服务器异常';
                    }
               }
          getToast(msg,2000);
            break;
        case 10:
          getToast(msg,2000);
            break;
        case -2:
        api.sendEvent({
            name: 'app_equipment',
            extra: {
                type: 226,
            }
        });
          break;
        case 1:
          getToast(msg,2000);
          break;
        default:
            if(code == undefined || msg == undefined){
                getToast('服务器异常',2000);
            }else {
                getToast(msg+':code='+code,2000);
            }

      }
    },

    //打印日志 和写日志文件
    getConsole:function(sign,msg){
      console.log('..sign..'+sign+'...msg...'+msg+'..time..'+new Date().toLocaleString());
    }

};
 //吐司
 function getToast(msg,duration){
   api.toast({
       msg: msg,
       duration: duration,
       location: 'bottom'
   });
 }
